package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.autocofiguration.util.OssUtil2;
import com.jmp.base.WebsiteApi;
import com.jmp.base.dto.*;
import com.jmp.feign.MessageFeign;
import one.stand.advice.ResultEnum;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CompanyVideoMapperExt;
import one.stand.mapper.DemandMapperExt;
import one.stand.model.*;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.user.UserAdminService;
import one.stand.util.AssertUtil;
import one.stand.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author cjr
 */
@RestController
public class WebsiteController implements WebsiteApi {

    @Autowired
    private MessageFeign messageFeign;

    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private UserAdminService userAdminService;


    @Autowired
    private CompanyVideoMapperExt companyVideoMapperExt;
    @Autowired
    private DemandMapperExt demandMapperExt;

    @Override
    public ResultModel<WebsiteInfoVo> info(@RequestBody WebsiteInfoDto request) {
        return ResultModel.success();
    }

    @Override
    public ResultModel save(@RequestBody WebsiteSaveDto request) {
        return ResultModel.success();
    }

    @Override
    public ResultModel<WebsiteVideoVo> video(@RequestBody WebsiteVideoDto request) {

        List<CompanyVideo> videos = companyVideoMapperExt.select(request.getCompanyId());
        List<WebsiteVideoVo.Video> videoList = ConvertUtils.list(videos, video -> {
            return WebsiteVideoVo.Video.builder()
                    .videoId(video.getVideoId())
                    .describes(video.getDescribes())
                    .url(video.getUrl())
                    .cover(video.getCover())
                    .sort(video.getSort())
                    .build();
        });

        WebsiteVideoVo vo = WebsiteVideoVo.builder()
                .videoList(videoList)
                .build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel videoSave(@RequestBody WebsiteVideoSaveDto request) {
        AssertUtil.nonNull(request.getUrl(), ResultEnum.PARAM_CHECK);

        CompanyVideo model = new CompanyVideo();
        model.setUrl(OssUtil2.getWebsiteUrlReplace(request.getUrl()));
        model.setDescribes(request.getDescribes());
        model.setCover(OssUtil2.getWebsiteUrlReplace(request.getCover()));

        if (request.getVideoId() == null) {
            // 新增官网视频
            model.setCompanyId(request.getCompanyId());
            model.setSort(companyVideoMapperExt.getMaxVideoSort(request.getCompanyId()) + 1);
            model.setIsDel(0);
            model.setCreateTime(DateUtil.currentDate());
            companyVideoMapperExt.insertSelective(model);
        } else {
            //  修改官网视频
            model.setVideoId(request.getVideoId());
            companyVideoMapperExt.updateByPrimaryKeySelective(model);
        }

        return ResultModel.success();
    }

    @Override
    public ResultModel videoRemove(@RequestBody WebsiteVideoRemoveDto request) {
        companyVideoMapperExt.updateByPrimaryKeySelective(new CompanyVideo() {{
            setVideoId(request.getVideoId());
            setIsDel(1);
        }});
        return ResultModel.success();
    }

    @Override
    public ResultModel videoMove(@RequestBody WebsiteVideoMoveDto request) {
        AssertUtil.nonNull(request.getVideoId(), ResultEnum.PARAM_CHECK);

        // 查询被移动的视频信息
        CompanyVideo companyVideoLast = companyVideoMapperExt.getThanVideoInfo(request.getVideoId(), request.isUp() ? 1 : 2);
        if (companyVideoLast == null) {
            if (request.isUp()) {
                return ResultModel.fail("到顶了，无法上移！");
            } else {
                return ResultModel.fail("到底了，无法下移！");
            }
        }
        CompanyVideo modelOpt = companyVideoMapperExt.selectByPrimaryKey(request.getVideoId());//移动的视频

        // 修改公司视频排序
        CompanyVideo model = new CompanyVideo();
        model.setVideoId(request.getVideoId());
        model.setSort(companyVideoLast.getSort());
        int i = companyVideoMapperExt.updateByPrimaryKeySelective(model);
        AssertUtil.isTrue(i > 0, ResultEnum.FAIL);
        model = new CompanyVideo();
        model.setVideoId(companyVideoLast.getVideoId());
        model.setSort(modelOpt.getSort());
        i = companyVideoMapperExt.updateByPrimaryKeySelective(model);
        AssertUtil.isTrue(i > 0, ResultEnum.FAIL);

        return ResultModel.success();
    }

    @Override
    public ResultModel<WebsiteDemandVo> demand(@RequestBody WebsiteDemandDto request) {

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Demand> list = demandMapperExt.select(request.getCompanyId());
        PageResponse2<WebsiteDemandVo.Demand> demandVoPageResponse2 = ConvertUtils.page2(list, demandModel -> {
            return WebsiteDemandVo.Demand.builder()
                    .log(demandModel.getLog())
                    .name(demandModel.getName())
                    .phone(demandModel.getPhone())
                    .text(demandModel.getText())
                    .build();
        });

        WebsiteDemandVo vo = WebsiteDemandVo.builder().demandPage(demandVoPageResponse2).build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel demandSave(@RequestBody WebsiteDemandSaveDto request) {
        BusinessCard card = businessCardBaseService.getCustomer(request.getCardId());

        Demand model = new Demand();
        model.setCardId(request.getCardId());
        model.setCompanyId(card.getCompanyId());
        model.setName(request.getName());
        model.setPhone(request.getPhone());
        model.setText(request.getText());
        model.setCreateTime(new Date());
        model.setIsDel(0);
        model.setLog(userAdminService.get(request.getOptUserId()).getUserImg());
        model.setLog(OssUtil2.getWebsiteUrlReplace(model.getLog()));
        int r = demandMapperExt.insertSelective(model);
        AssertUtil.isTrue(r > 0, ResultEnum.FAIL);

        // 咨询回复通知微信推送提醒
        String[] fillData = {model.getName(), DateUtil.getDay(), model.getText()};
        MessagePushRequest pushRequest = MessagePushRequest.builder()
                .type(MessagePushType.FROM_SUBMIT)
                .toId(card.getUserId())
                .fillData(fillData)
                .build();
        messageFeign.push(pushRequest);

        return ResultModel.success();
    }
}
